package com.haiya.common;

/**
 * @author qiaoguoqiang
 * 断言关注点，提供常见参数、状态断言
 */
public class AssertionConcern {
    protected AssertionConcern() {
        super();
    }

    /**
     * 断言参数相等
     * @param anObject1 对象1
     * @param anObject2 对象2
     * @param aMessage  断言失败的信息
     */
    protected void assertArgumentEquals(Object anObject1, Object anObject2, String aMessage) {
        if (!anObject1.equals(anObject2)) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言参数为false
     * @param aBoolean
     * @param aMessage
     */
    protected void assertArgumentFalse(boolean aBoolean, String aMessage) {
        if (aBoolean) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言字符串参数长度是否超长
     * @param aString
     * @param aMaximum
     * @param aMessage
     */
    protected void assertArgumentLength(String aString, int aMaximum, String aMessage) {
        int length = aString.trim().length();
        if (length > aMaximum) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言字符串参数长度是否在给定区间
     * @param aString
     * @param aMinimum
     * @param aMaximum
     * @param aMessage
     */
    protected void assertArgumentLength(String aString, int aMinimum, int aMaximum, String aMessage) {
        int length = aString.trim().length();
        if (length < aMinimum || length > aMaximum) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言字符串非空
     * @param aString
     * @param aMessage
     */
    protected void assertArgumentNotEmpty(String aString, String aMessage) {
        if (aString == null || aString.trim().isEmpty()) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言两个对象不相等
     * @param anObject1
     * @param anObject2
     * @param aMessage
     */
    protected void assertArgumentNotEquals(Object anObject1, Object anObject2, String aMessage) {
        if (anObject1.equals(anObject2)) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言对象非Null
     * @param anObject
     * @param aMessage
     */
    protected void assertArgumentNotNull(Object anObject, String aMessage) {
        if (anObject == null) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言双精度数参数范围
     * @param aValue
     * @param aMinimum
     * @param aMaximum
     * @param aMessage
     */
    protected void assertArgumentRange(double aValue, double aMinimum, double aMaximum, String aMessage) {
        if (aValue < aMinimum || aValue > aMaximum) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言浮点数参数范围
     * @param aValue
     * @param aMinimum
     * @param aMaximum
     * @param aMessage
     */
    protected void assertArgumentRange(float aValue, float aMinimum, float aMaximum, String aMessage) {
        if (aValue < aMinimum || aValue > aMaximum) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言整型参数范围
     * @param aValue
     * @param aMinimum
     * @param aMaximum
     * @param aMessage
     */
    protected void assertArgumentRange(int aValue, int aMinimum, int aMaximum, String aMessage) {
        if (aValue < aMinimum || aValue > aMaximum) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言长整型参数范围
     * @param aValue
     * @param aMinimum
     * @param aMaximum
     * @param aMessage
     */
    protected void assertArgumentRange(long aValue, long aMinimum, long aMaximum, String aMessage) {
        if (aValue < aMinimum || aValue > aMaximum) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言参数是否为真
     * @param aBoolean
     * @param aMessage
     */
    protected void assertArgumentTrue(boolean aBoolean, String aMessage) {
        if (!aBoolean) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    /**
     * 断言状态是否为假
     * @param aBoolean
     * @param aMessage
     */
    protected void assertStateFalse(boolean aBoolean, String aMessage) {
        if (aBoolean) {
            throw new IllegalStateException(aMessage);
        }
    }

    /**
     * 断言状态是否为真
     * @param aBoolean
     * @param aMessage
     */
    protected void assertStateTrue(boolean aBoolean, String aMessage) {
        if (!aBoolean) {
            throw new IllegalStateException(aMessage);
        }
    }
}
